package com.njupt.Tree;



import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.DelayQueue;

/**
 * @Author: wujiaming
 * @CreateTime: 2024/11/21 9:35
 * @Description: 429.N叉树的层序遍历
 * @Version: 1.0
 */


public class NlevelOrder {

    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> results = new ArrayList<>();
        Deque<Node> queue = new LinkedList<>();
        if(root == null) return results;
        queue.addLast(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> result = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Node node = queue.pollFirst();
                result.add(node.val);
                if(node.children != null){
                    for (Node child : node.children) {
                        queue.addLast(child);
                    }
                }

            }
            results.add(result);
        }
        return results;
    }

    public static void main(String[] args) {
        Node node1 = new Node(1);
        Node node3 = new Node(3);
        Node node2 = new Node(2);
        Node node4 = new Node(4);
        Node node5 = new Node(5);
        Node node6 = new Node(6);

        List<Node> nodes1 = new ArrayList<>();
        nodes1.add(node3);
        nodes1.add(node2);
        nodes1.add(node4);
        node1.children = nodes1;

        List<Node> nodes3 = new ArrayList<>();
        nodes3.add(node5);
        nodes3.add(node6);
        node3.children = nodes3;

        NlevelOrder test = new NlevelOrder();
        List<List<Integer>> lists = test.levelOrder(node1);
        System.out.println(lists);
    }
}
